#include "connection.hpp"
#include <iostream>

//这里函数有四个参数, 是因为该回调函数不单单发挥数据的作用, 还需要其顺便发挥basicAck
void cb(const MyRabbitMQ::Channel::ptr& cptr, const std::string& consumer_tag, const protoMQ::BaseProperties* base, const std::string& body)
{
    //测试消息确认
    cptr->basicAck(base->id());
    LOG(logLevel::INFO) << "消费者 " << consumer_tag << " 消费了消息:" << body << std::endl;
}

void cb_2(const MyRabbitMQ::Channel::ptr& cptr, const std::string& consumer_tag, const protoMQ::BaseProperties* base, const std::string& body)
{
    //测试消息确认
    cptr->basicAck(base->id());
    LOG(logLevel::INFO) << "消费者(云山) " << consumer_tag << " 消费了消息:" << body << std::endl;
}

// int main()
// {
//     //实例化异步工作线程对象
//     MyRabbitMQ::AsyncWorker::ptr aw = std::make_shared<MyRabbitMQ::AsyncWorker>();
    
//     //实例化连接对象
//     MyRabbitMQ::Connection::ptr connect = std::make_shared<MyRabbitMQ::Connection>("127.0.0.1",8085,aw);
    
//     //通过连接对象实例化信道对象(自动发送开启信道请求)
//     MyRabbitMQ::Channel::ptr channel = connect->createChannel();


//     google::protobuf::Map<std::string, std::string> args;
    
//     //声明交换机(因为不能保证交换机一定存在)
//     channel->declareExchange("exchange-1",protoMQ::ExchangeType::fanout,false,false,args);

//     //声明队列(因为不能保证队列一定存在)
//     channel->declareQueue("queue-1",false,false,false,args);
//     //channel->declareQueue("queue-2",false,false,false,args);

//     //绑定
//     channel->bind("exchange-1","queue-1","rivellon");
//    // channel->bind("exchange-1","queue-2","hello");

//     //订阅队列消息
//     channel->queueConsume("queue-1","consumer-1",false,std::bind(cb, channel, std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));

//     //循环等待消息
//     while(1)
//     {
//         std::this_thread::sleep_for(std::chrono::seconds(3));
//     }

//     //关闭信道(自动发送关闭信道请求)
//     connect->deleteChannel(channel->getChannelId());

//     connect.reset();

//     return 0;
// }

int main()
{
    //实例化异步工作线程对象
    MyRabbitMQ::AsyncWorker::ptr aw = std::make_shared<MyRabbitMQ::AsyncWorker>();
    //实例化连接对象
    MyRabbitMQ::Connection::ptr connect = std::make_shared<MyRabbitMQ::Connection>("127.0.0.1",8085,aw);
    //通过连接对象实例化信道对象(自动发送开启信道请求)
    MyRabbitMQ::Channel::ptr channel = connect->createChannel();
    google::protobuf::Map<std::string, std::string> args;
    


    //声明交换机(广播模式)
    channel->declareExchange("exchange-1",protoMQ::ExchangeType::fanout,false,false,args);

    //声明队列
    channel->declareQueue("queue-1",false,false,false,args);
    channel->declareQueue("queue-2",false,false,false,args);

    //绑定
    //channel->bind("exchange-1","queue-1","rivellon");
    channel->bind("exchange-1","queue-2","云山");//这里应该收不到任何信息, 因为bindkey不合法

    //订阅队列消息
    channel->queueConsume("queue-2","consumer-1",false,std::bind(cb_2, channel, std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));
    //channel->queueConsume("queue-1","consumer-1",false,std::bind(cb, channel, std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));

    //循环等待消息
    while(1)
    {
        std::this_thread::sleep_for(std::chrono::seconds(3));
    }

    //关闭信道(自动发送关闭信道请求)
    connect->deleteChannel(channel->getChannelId());

    connect.reset();

    return 0;
}